package com.xiaren.ui.tree;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

import com.xiaren.bean.FileBean;
import com.xiaren.ui.tree.JTreeNode;
import com.xiaren.common.IConstant;
import com.xiaren.common.ObjectConstant;
import com.xiaren.ui.LeftJPanel;
import com.xiaren.ui.tree.treedrag.TreeDropTarget;

/**
 * @主页 http://www.leaf123.cn/
 * @邮箱 xia5523@qq.com
 */
public class JTreeBiz {

	public static long ROOT_PARENT = -1;
	public LeftJPanel leftPanel = null;

	public static JTreeNode rootNote = new JTreeNode(
			"root                                     ", ROOT_PARENT);
	public static JTreeNode selectNote = null;

	public JTreeBiz() {
		super();
		ObjectConstant.treeBiz = this;
		this.leftPanel = ObjectConstant.leftPanel;
		ObjectConstant.mainTree = createTree();
		// ObjectConstant.mainTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);//一次只能选择一个路径。
		new TreeMouseRight(ObjectConstant.mainTree);
		ObjectConstant.mainTree
				.addTreeSelectionListener(new TreeSelectionImpl());

		TreeDropTarget.tt();// 添加树节点的文档拖拽功能
		// TransferableTreeNode.toAddTreeDrag();
	}

	/**
	 * 根据文件，构造一棵树
	 *
	 * @return
	 */
	private JTree createTree() {
		List<JTreeNode> listNote = new ArrayList<>();
		List<FileBean> fileBeanList = ObjectConstant.mainFrame.baseDao.getDirectory();
		for (FileBean fileBean : fileBeanList){
			JTreeNode treeNote = new JTreeNode(fileBean.getId(), fileBean.getParentId(), fileBean.getName());
			treeNote.setPassword(fileBean.getPassword());
			listNote.add(treeNote);
			if (treeNote.parentId == -1) {
				rootNote = treeNote;
				rootNote.ifFind = true;
			}
		}

		getChilds(rootNote, listNote);
		for (JTreeNode treeNote : listNote) {
			treeNote.ifFind = false;
		}
		JTree tree = new JTree(rootNote);


		return tree;
	}

	private void getChilds(JTreeNode treeNote, List<JTreeNode> listNote) {
		for (JTreeNode note : listNote) {
			if (!note.ifFind && note != treeNote) {
				if (note.parentId == treeNote.treeNodeId) {
					note.ifFind = true;
					treeNote.add(note);
					getChilds(note, listNote);
				}
			}
		}
	}

	/**
	 * 将树节点中的所有数据，变成list集合
	 *
	 * @param root
	 * @return
	 */
	public List<JTreeNode> noteToList(JTreeNode root) {
		List<JTreeNode> listNote = new ArrayList<JTreeNode>();
		listNote.add(root);
		Enumeration arr = root.children();
		while (arr.hasMoreElements()) {
			listNote.addAll(noteToList((JTreeNode) arr.nextElement()));
		}
		return listNote;
	}

//	/**
//	 * 根据文件内容，得到treenote的节点集合
//	 *
//	 * @param content
//	 * @return
//	 */
//	public List<JTreeNode> fileToList(String content) {
//		List<JTreeNode> listNote = new ArrayList<JTreeNode>();
//		String[] arrStrNote = content.split("\r\n");
//		for (String string : arrStrNote) {
//			String[] strNote = string.split(",");
//			listNote.add(new JTreeNode(Long.parseLong(strNote[0]), Long
//					.parseLong(strNote[1]), strNote[2]));
//		}
//		return listNote;
//	}

	/**
	 *
	 * @param parent 目标节点
	 * @param node 拖动的节点
	 * @return
	 */
	public static boolean canTrag(JTreeNode parent, JTreeNode node) {
		if(isParent(node, parent)){//目标节点不能是拖动节点的子节点或者子节点一下的节点
			return false;
		}
		if(parent.treeNodeId == node.treeNodeId){//不能是同一个节点
			return false;
		}
		return true;
	}
	/**
	 * 判断parent是否是node的父节点，或者父节点以上的节点
	 * @param parent
	 * @param node
	 * @return
	 */
	public static boolean isParent(JTreeNode parent, JTreeNode node) {
		while (node != rootNote) {
			if (parent.treeNodeId == node.parentId) {
				return true;
			}
			node = (JTreeNode) node.getParent();
		}
		return false;
	}

}
